<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
<!--      <a-button type="primary" icon="download" @click="handleExportXls('科室')">导出</a-button>-->
<!--      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"-->
<!--        @change="handleImportExcel">-->
<!--        <a-button type="primary" icon="import">导入</a-button>-->
<!--      </a-upload>-->
      <!-- 高级查询区域 -->
<!--      <j-super-query :fieldList="superFieldList" ref="superQueryModal"-->
<!--        @handleSuperQuery="handleSuperQuery"></j-super-query>-->
<!--      <a-button type="primary" icon="check-circle" style="margin-left: 8px" @click="submitAllData">全部提交审核</a-button>-->
      <a-button type="primary" icon="check-circle" style="margin-left: 8px" @click="batchSubmit">批量提交</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{
          selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table ref="table" size="middle" :scroll="{ x: true }" bordered rowKey="id" :columns="columns"
        :dataSource="dataSource" :pagination="ipagination" :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt=""
            style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <template slot="action" slot-scope="text, record" class="action-buttons">
          <!-- 1. 编辑按钮：排除【通过】【待审批】状态 -->
          <template v-if="record.status !== '通过' && record.status !== '待审批'&& record.status !== '第一人审批同意'">
            <a @click="handleEdit(record)" class="action-item">编辑</a>
          </template>

          <!-- 2. 编辑-提交分界线：仅在【有编辑按钮】且【有提交按钮】时显示 -->
          <template v-if="record.status === '驳回' || record.status === '待提交'">
            <span class="divider">|</span>
          </template>

          <!-- 3. 提交按钮：仅【驳回】【待提交】显示 -->
          <template v-if="(record.status === '驳回' || record.status === '待提交') && record.status !== '通过'&& record.status !== '第一人审批同意'">
            <a-popconfirm
              title="确定提交吗?"
              @confirm="() => handleSubmit(record.id)"
            >
              <a class="action-item">提交</a>
            </a-popconfirm>
          </template>

          <!-- 4. 操作-审批详情分界线：仅在【有编辑/提交按钮】时显示 -->
          <template v-if="record.status === '驳回' || record.status === '待提交'">
            <span class="divider">|</span>
          </template>

          <!-- 5. 审批详情：所有状态均显示（待审批时仅显示此内容） -->
          <a @click="openApprovalMessageDrawer(record.id)" class="action-item">审批详情</a>
        </template>

      </a-table>
    </div>
    <!-- table区域 end -->
    <!-- 审批详情抽屉 -->
    <cw-approval-message-drawer
      ref="approvalMessageDrawer"
      @ok="handleApprovalMessageOk"
    ></cw-approval-message-drawer>


    <cw-ks-modal ref="modalForm" @ok="modalFormOk"></cw-ks-modal>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import CwKsModal from './modules/CwKsModal'
import CwApprovalMessageDrawer from './modules/CwApprovalMessageModal.Style#Drawer.vue'

export default {
  name: 'CwKsList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    CwApprovalMessageDrawer,
    CwKsModal
  },
  data() {
    return {
      description: '科室管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '科室名称',
          align: "center",
          dataIndex: 'ksmc'
        },
        {
          title: '年度',
          align: "center",
          dataIndex: 'nd'
        },
        {
          title: '单位',
          align: "center",
          dataIndex: 'dw'
        },
        {
          title: '办公费',
          align: "center",
          dataIndex: 'bgf'
        },
        {
          title: '印刷费',
          align: "center",
          dataIndex: 'ys'
        },
        {
          title: '手续费',
          align: "center",
          dataIndex: 'sx'
        },
        {
          title: '水费',
          align: "center",
          dataIndex: 'sf'
        },
        {
          title: '电费',
          align: "center",
          dataIndex: 'df'
        },
        {
          title: '邮电费',
          align: "center",
          dataIndex: 'yd'
        },
        {
          title: '取暖费',
          align: "center",
          dataIndex: 'qn'
        },
        {
          title: '物业管理费',
          align: "center",
          dataIndex: 'wy'
        },
        {
          title: '差旅费',
          align: "center",
          dataIndex: 'cl'
        },
        {
          title: '因公出国（境）费用',
          align: "center",
          dataIndex: 'cg'
        },
        {
          title: '维修（护）费',
          align: "center",
          dataIndex: 'wxf'
        },
        {
          title: '租赁费',
          align: "center",
          dataIndex: 'zl'
        },
        {
          title: '会议费',
          align: "center",
          dataIndex: 'hy'
        },
        {
          title: '培训费',
          align: "center",
          dataIndex: 'px'
        },
        {
          title: '公务接待费',
          align: "center",
          dataIndex: 'gwjd'
        },
        {
          title: '专用材料费',
          align: "center",
          dataIndex: 'zycl'
        },
        {
          title: '被装购置费',
          align: "center",
          dataIndex: 'bz'
        },
        {
          title: '专用燃料费',
          align: "center",
          dataIndex: 'zyll'
        },
        {
          title: '劳务费',
          align: "center",
          dataIndex: 'lw'
        },
        {
          title: '委托业务费',
          align: "center",
          dataIndex: 'wt'
        },
        {
          title: '工会经费',
          align: "center",
          dataIndex: 'gh'
        },
        {
          title: '公务用车运行维护费',
          align: "center",
          dataIndex: 'gwc'
        },
        {
          title: '其他交通费用',
          align: "center",
          dataIndex: 'qtjtfy'
        },
        {
          title: '税金及附加费用',
          align: "center",
          dataIndex: 'sj'
        },
        {
          title: '其他商品和服务支出',
          align: "center",
          dataIndex: 'qtsp'
        },
        {
          title: '房屋建筑物购建',
          align: "center",
          dataIndex: 'fw'
        },
        {
          title: '办公设备购置',
          align: "center",
          dataIndex: 'bg'
        },
        {
          title: '专用设备购置',
          align: "center",
          dataIndex: 'zy'
        },
        {
          title: '基础设施建设',
          align: "center",
          dataIndex: 'jc'
        },
        {
          title: '大型修缮',
          align: "center",
          dataIndex: 'dx'
        },
        {
          title: '信息网络及软件购置更新',
          align: "center",
          dataIndex: 'xx'
        },
        {
          title: '物资储备',
          align: "center",
          dataIndex: 'wz'
        },
        {
          title: '土地补偿',
          align: "center",
          dataIndex: 'td'
        },
        {
          title: '安置补助',
          align: "center",
          dataIndex: 'az'
        },
        {
          title: '地上附着物和青苗补偿',
          align: "center",
          dataIndex: 'ds'
        },
        {
          title: '拆迁补偿',
          align: "center",
          dataIndex: 'cq'
        },
        {
          title: '公务用车购置',
          align: "center",
          dataIndex: 'gw'
        },
        {
          title: '其他交通工具购置',
          align: "center",
          dataIndex: 'qtjt'
        },
        {
          title: '文物和陈列品购置',
          align: "center",
          dataIndex: 'ww'
        },
        {
          title: '无形资产购置',
          align: "center",
          dataIndex: 'wx'
        },
        {
          title: '其他资本性支出',
          align: "center",
          dataIndex: 'qt'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          fixed: "right",
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: "/cw/cwKs/list",
        delete: "/cw/cwKs/delete",
        deleteBatch: "/cw/cwKs/deleteBatch",
        exportXlsUrl: "/cw/cwKs/exportXls",
        importExcelUrl: "cw/cwKs/importExcel",
        submitBatch: "/cw/cwKs/submitBatch",   // 批量提交审核
        tj: '/cw/cwKs/tj'
      },
      dictOptions: {},
      superFieldList: [],
    }
  },
  created() {
    this.getSuperFieldList();
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    },
  },
  methods: {
// 提交处理方法
    handleSubmit(id) {
      if (!id) {
        this.$message.error('缺少记录ID')
        return
      }

      this.loading = true
      this.$http({
        url: this.url.tj,
        method: 'post',
        headers: this.tokenHeader,
        params: { id: id }
      }).then(res => {
        if (res.success) {
          this.$message.success('提交成功！')
          this.loadData() // 刷新列表
        } else {
          this.$message.error(res.message || '提交失败')
        }
      }).catch(err => {
        this.$message.error('提交出错：' + (err.message || err.toString()))
      }).finally(() => {
        this.loading = false
      })
    },
    // 打开审批详情抽屉
    openApprovalMessageDrawer(recordId) {
      this.$refs.approvalMessageDrawer.open(recordId)
    },
    // 审批详情抽屉确认回调
    handleApprovalMessageOk() {
      // 可在此处处理刷新逻辑（如刷新当前列表）
    },
    initDictConfig() {
    },
    /**
    * 1. 单个数据提交审核
    * @param id 单个数据ID
    */
    submitSingle(id) {
      this.$confirm({
        title: '提交确认',
        content: '确定要提交这条科室数据进行审核吗？',
        okText: '确认',
        cancelText: '取消',
        onOk: () => {
          this.loading = true;
          // 调用接口：参数拼接到URL（id + status）
          this.$http({
            url: this.url.submitSingle,
            method: 'put',
            params: { id: id, status: '待审批' }
          }).then(res => {
            if (res.success) {
              this.$message.success('提交审核成功！');
              this.loadData(); // 重新加载表格数据
            } else {
              this.$message.error('提交失败：' + (res.message || '系统异常'));
            }
          }).finally(() => {
            this.loading = false;
          });
        }
      });
    },

    /**
     * 2. 批量数据提交审核
     */
    batchSubmit() {
      if (this.selectedRowKeys.length === 0) {
        this.$message.warning('请先勾选要提交的数据！');
        return;
      }

      this.$confirm({
        title: '批量提交确认',
        content: `确定要提交选中的${this.selectedRowKeys.length}条科室数据吗？`,
        okText: '确认',
        cancelText: '取消',
        onOk: () => {
          this.loading = true;
          // 调用接口：ids用逗号拼接，参数拼接到URL
          this.$http({
            url: this.url.submitBatch,
            method: 'put',
            params: {
              ids: this.selectedRowKeys.join(','),
              status: '待审批'
            }
          }).then(res => {
            if (res.success) {
              this.$message.success('批量提交审核成功！');
              this.selectedRowKeys = []; // 清空选中状态
              this.loadData(); // 重新加载表格数据
            } else {
              this.$message.error('批量提交失败：' + (res.message || '系统异常'));
            }
          }).finally(() => {
            this.loading = false;
          });
        }
      });
    },

    /**
     * 3. 全部数据提交审核
     */
    submitAllData() {
      this.$confirm({
        title: '全部提交确认',
        content: '确定要将所有科室数据提交审核吗？（包括已提交的数据会重复更新状态）',
        okText: '确认',
        cancelText: '取消',
        onOk: () => {
          this.loading = true;
          // 调用接口：仅传递status参数
          this.$http({
            url: this.url.submitAll,
            method: 'put',
            params: { status: '待审批' }
          }).then(res => {
            if (res.success) {
              this.$message.success('全部数据提交审核成功！');
              this.loadData(); // 重新加载表格数据
            } else {
              this.$message.error('全部提交失败：' + (res.message || '系统异常'));
            }
          }).finally(() => {
            this.loading = false;
          });
        }
      });
    },
    getSuperFieldList() {
      let fieldList = [];
      fieldList.push({ type: 'string', value: 'ksmc', text: '科室名称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'jr', text: '金额', dictCode: '' })
      fieldList.push({ type: 'string', value: 'csnr', text: '测算内容', dictCode: '' })
      fieldList.push({ type: 'string', value: 'csyj', text: '测算依据', dictCode: '' })
      fieldList.push({ type: 'string', value: 'nd', text: '年度', dictCode: '' })
      fieldList.push({ type: 'string', value: 'dw', text: '单位', dictCode: '' })
      fieldList.push({ type: 'string', value: 'bgf', text: '办公费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'ys', text: '印刷费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'sx', text: '手续费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'sf', text: '水费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'df', text: '电费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'yd', text: '邮电费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'qn', text: '取暖费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'wy', text: '物业管理费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'cl', text: '差旅费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'cg', text: '因公出国（境）费用', dictCode: '' })
      fieldList.push({ type: 'string', value: 'wxf', text: '维修（护）费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'zl', text: '租赁费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'hy', text: '会议费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'px', text: '培训费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'gwjd', text: '公务接待费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'zycl', text: '专用材料费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'bz', text: '被装购置费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'zyll', text: '专用燃料费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'lw', text: '劳务费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'wt', text: '委托业务费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'gh', text: '工会经费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'gwc', text: '公务用车运行维护费', dictCode: '' })
      fieldList.push({ type: 'string', value: 'qtjtfy', text: '其他交通费用', dictCode: '' })
      fieldList.push({ type: 'string', value: 'sj', text: '税金及附加费用', dictCode: '' })
      fieldList.push({ type: 'string', value: 'qtsp', text: '其他商品和服务支出', dictCode: '' })
      fieldList.push({ type: 'string', value: 'fw', text: '房屋建筑物购建', dictCode: '' })
      fieldList.push({ type: 'string', value: 'bg', text: '办公设备购置', dictCode: '' })
      fieldList.push({ type: 'string', value: 'zy', text: '专用设备购置', dictCode: '' })
      fieldList.push({ type: 'string', value: 'jc', text: '基础设施建设', dictCode: '' })
      fieldList.push({ type: 'string', value: 'dx', text: '大型修缮', dictCode: '' })
      fieldList.push({ type: 'string', value: 'xx', text: '信息网络及软件购置更新', dictCode: '' })
      fieldList.push({ type: 'string', value: 'wz', text: '物资储备', dictCode: '' })
      fieldList.push({ type: 'string', value: 'td', text: '土地补偿', dictCode: '' })
      fieldList.push({ type: 'string', value: 'az', text: '安置补助', dictCode: '' })
      fieldList.push({ type: 'string', value: 'ds', text: '地上附着物和青苗补偿', dictCode: '' })
      fieldList.push({ type: 'string', value: 'cq', text: '拆迁补偿', dictCode: '' })
      fieldList.push({ type: 'string', value: 'gw', text: '公务用车购置', dictCode: '' })
      fieldList.push({ type: 'string', value: 'qtjt', text: '其他交通工具购置', dictCode: '' })
      fieldList.push({ type: 'string', value: 'ww', text: '文物和陈列品购置', dictCode: '' })
      fieldList.push({ type: 'string', value: 'wx', text: '无形资产购置', dictCode: '' })
      fieldList.push({ type: 'string', value: 'qt', text: '其他资本性支出', dictCode: '' })
      this.superFieldList = fieldList
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>